import { ref, onMounted, onUnmounted } from 'vue';

export function useNetworkStatus() {
  const isOnline = ref(false);
  let intervalId = null;

  const updateOnlineStatus = async () => {
    console.log('window.electronAPI:', window.electronAPI);

    if (window.electronAPI && window.electronAPI.checkNetworkStatus) {
      try {
        const status = await window.electronAPI.checkNetworkStatus();
        console.log('Network status from main process:', status);
        isOnline.value = status;
      } catch (error) {
        console.error('Error checking network status:', error);
        isOnline.value = false;
      }
    } else {
      console.warn('electronAPI not available, using navigator.onLine');
      isOnline.value = navigator.onLine;
    }
  };

  onMounted(() => {
    updateOnlineStatus();
    intervalId = setInterval(updateOnlineStatus, 100000);
  });

  onUnmounted(() => {
    clearInterval(intervalId);
  });

  return { isOnline };
}
